package com.miyake.client.datamanage;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.miyake.client.tools.SmallButton;

abstract public class ToolBar extends Composite {

	private List<ToolBarHandler> handlers = new ArrayList<ToolBarHandler>();
	private HorizontalPanel horizontalPanel;

	protected void fireReloadEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onReload();
		}
	}
	
	protected void fireNewRootEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewRoot();
		}
	}
	
	protected void fireNewFolderEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewFolder();
		}
	}
	
	protected void fireNewDataEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewData();
		}
	}
	
	protected void fireDeleteEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onDelete();
		}		
	}
	
	protected void fireNewPlotEvent(Integer columns) {
		for (ToolBarHandler handler : handlers) {
			handler.onNewPlot(columns);
		}		
	}

	protected void fireNewTableEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewTable();
		}		
	}
	
	protected void fireNewTextEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewText();
		}		
	}
	
	protected void fireUpEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onUp();
		}		
	}
	
	protected void fireDownEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onDown();
		}		
	}

	protected void fireNewImageEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewImage();
		}		
	}
	
	protected void fireNewPaperEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onNewPaper();
		}		
	}

	protected void firePostEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onPost();
		}		
	}
	
	protected void fireRenameEvent() {
		for (ToolBarHandler handler : handlers) {
			handler.onRename();
		}		
	}
	
	abstract protected String getActiveKey();
	
	public ToolBar(ToolBarHandler handler) {
		handlers.add(handler);
		horizontalPanel = new HorizontalPanel();
		initWidget(horizontalPanel);
		//horizontalPanel.setWidth("100%");
		horizontalPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		addButton("Reload", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireReloadEvent();
			}
		});
		
		addButton("Project", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewRootEvent();
			}
		});
		
		addButton("Folder", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewFolderEvent();
			}
		});
		
/*		addButton("Data", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewDataEvent();
			}
		});
		
		addButton("Image", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewImageEvent();
			}
		});
*/
		addButton("Paper", new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				fireNewPaperEvent();
			}
		});
		
		addButton("Delete", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireDeleteEvent();
			}
		});
/*		
		addButton("Chart", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewPlotEvent();
			}
			
		});
		
		addButton("Table", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewTableEvent();
			}
			
		});
		
		addButton("Text", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireNewTextEvent();
			}	
		});
*/		
		addButton("Up", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireUpEvent();
			}	
		});
		
		addButton("Down", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireDownEvent();
			}	
		});
		
		addButton("Post", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				firePostEvent();
			}	
		});
		
		addButton("Rename", new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				fireRenameEvent();
			}
		});
	}

	public void addButton(String name, ClickHandler handler) {
		SmallButton button = new SmallButton(name);
		//button.setStylePrimaryName("tglbtnUpText");
		button.addClickHandler(handler);
		horizontalPanel.add(button);
	}
	
}
